//
//  ZJFindpswSenconViewController.m
//  ZiJinLian
//
//  Created by lzy on 2017/3/9.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ZJFindPswSecondViewController.h"
#import "ZJSetPasswordView.h"


@interface ZJFindPswSecondViewController ()<UITextFieldDelegate>

@end

@implementation ZJFindPswSecondViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
}
-(void)backAction{
    UIViewController *viewCtl = self.navigationController.viewControllers[0];
    [self.navigationController popToViewController:viewCtl animated:YES];
}
-(void)createView{
    [self initTitle:MyString(@"找回密码")];
    self.view.userInteractionEnabled = YES;
    _pswView=[[ZJSetPasswordView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:self.pswView];


}
-(void)doOpeartion{
    [self setdeleget];
    [self btnTarget];
    [self.pswView.firstTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self doKeyBoardDown];
}



#pragma -----------设置代理方法和 按钮target--------
-(void)setdeleget
{
    self.pswView.firstTF.delegate=self;
    self.pswView.secondTF.delegate=self;
    
}

-(void)btnTarget
{
    [self.pswView.sureBtn addTarget:self action:@selector(sureClick:) forControlEvents:UIControlEventTouchUpInside];
    
}
#pragma -----------输入框代理方法--------
//输入框代理方法
- (void) textFieldDidChange:(id) sender {
    UITextField *_field = (UITextField *)sender;
    NSString *str=_field.text;
    if (str) {
        [self.pswView.sureBtn setBackgroundColor:button_Red_Color];
        [self.pswView.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}


#pragma -----------键盘下降方法--------
-(BOOL) textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}



-(void)sureClick:(UIButton *)btn
{
    
    [self.view endEditing:YES];
    if (![regular checkRegular8:self.pswView.firstTF.text]||![regular checkRegular8:self.pswView.firstTF.text]) {
        
        
        return;
    }
    
    if (![self.pswView.firstTF.text isEqualToString:self.pswView.secondTF.text]) {
        [self showMessage:MyString(@"两次输入密码不一致")];
        return;
    }else{
        if(self.pswView.firstTF.text.length<6&&self.pswView.secondTF.text.length<6) {
            [self showMessage:MyString(@"密码过短")];
            return;
        }else{
            if ([self deptNumInputShouldNumber:self.pswView.firstTF.text]) {
                [self showMessage:MyString(@"请输入6-20位字母或数字密码")];
                return;
            }else{
                
            }
        }
    }

    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    [parametersDic setObject:self.curphoneStr forKey:@"phone"];
    [parametersDic setObject:self.pswView.secondTF.text forKey:@"password"];
    [parametersDic setObject:self.smscodeStr forKey:@"smsCode"];
    [self doHttpRequestWithApi:API_FIND_PSW method:PUT dic:parametersDic successBlock:^(NSDictionary *dic) {
        //储存密码
        [[NSUserDefaults standardUserDefaults]setObject:self.curphoneStr forKey:@"passwordabc"];
        //储存账户
        [[NSUserDefaults standardUserDefaults ]setObject:self.pswView.secondTF.text forKey:@"userNameabc"];
        //必须
        [[NSUserDefaults standardUserDefaults]synchronize];
        [MBProgressHUD showSuccess:@"找回成功,请用新密码登录!"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
        
    }];
 
}



@end
